package com.dai.zhenfujin.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.AlumFansListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.visitingcard.adapter.FansAdapter2;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FansActivity2 extends BaseActivity {

    private ListView listView;
    private ImageView iv_no_msg;
    private TextView tvName;
    private FrameLayout fl_img;
    private SmartRefreshLayout refreshLayout;
    private List<AlumFansListBean.MsgListBean> msg_list = new ArrayList<>();
    private FansAdapter2 adapter;
    private int page = 1;
    private int number = 10;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_fans_activity);
        initView();
    }

    private void initView() {
        tvName = findViewById(R.id.title_textview);
        fl_img = findViewById(R.id.fl_img);
        listView = findViewById(R.id.fans_listview);
        iv_no_msg = findViewById(com.dai.zhenfujin.R.id.iv_no_msg);
        refreshLayout = findViewById(com.dai.zhenfujin.R.id.refreshLayout);
        tvName.setText("我的福利");
        adapter = new FansAdapter2(this, msg_list);
        listView.setAdapter(adapter);
        fl_img.setOnClickListener(v -> finish());
        onRefresh();
        initRefresh();
    }


    private void onRefresh() {
        page = 1;
        getFansList(true);
    }

    private void initRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getFansList(true);
                        refreshLayout.finishRefresh();
                        refreshLayout.resetNoMoreData();
                    }
                },1500);
            }
        });

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                getFansList(false);
            }
        });
    }

    private void getFansList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("page", page + "");
        params2.put("number", number + "");
        params.put("req_bd",  params2);
        HttpClient.post(this, Constants.MSGLIST2, params, new CallBack<AlumFansListBean>() {
            @Override
            public void onSuccess(AlumFansListBean resultBean) {
               // SPUtils.put(FansActivity2.this,"flag",true);
                if (isRefresh) {
                    msg_list.clear();
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }

                if (resultBean.getMsg_list().size() > 0) {
                    msg_list.addAll(resultBean.getMsg_list());
                    adapter.notifyDataSetChanged();
                    if (page==2)
                        iv_no_msg.setVisibility(View.GONE);
                }else {
                    //  UIUtils.showToastSafe("暂无数据");
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                   // if (page==2)
                        //iv_no_msg.setVisibility(View.VISIBLE);
                }
            }
        });
      }
    }

